package ricky.scene
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.text.TextField;
	import flash.text.TextFormat;
	
	import ricky.events.CameraEvent;

	/**
	 * 小地图
	 */
	public class MiniMap extends Sprite
	{
		public function MiniMap()
		{
			_info=new TextField();
			_info.defaultTextFormat=new TextFormat(null,12,0xffffff,true);
			_info.autoSize="left";
		}
		
		public function init(mapdata:MapData,camera:Camera,bmpDate:BitmapData):void
		{
			_mapData=mapdata;
			_camera=camera;
			
			while(this.numChildren>0)
				this.removeChildAt(0);
			
			this.graphics.clear();
			
			this.graphics.beginBitmapFill(bmpDate,null,false);
			this.graphics.drawRect(0,0,bmpDate.width,bmpDate.height);
			this.graphics.endFill();

			this._biliX=bmpDate.width/mapdata.width;
			this._biliY=bmpDate.height/mapdata.height;
				
			_visibleArea=new Sprite();
			drawVisibleArea();
			this.addChild(_visibleArea);
			
			if(_camera)
			{
				_camera.removeEventListener(CameraEvent.Camera_Move,onCameraMoving);
				_camera.removeEventListener(CameraEvent.Camera_Resize,onCameraResize);
			}
			
			_camera.addEventListener(CameraEvent.Camera_Move,onCameraMoving);
			_camera.addEventListener(CameraEvent.Camera_Resize,onCameraResize);
			
			this.addChild(_info);
		}
		
		/**
		 * 显示信息到小地图上
		 * 支持html格式
		 */
		public function showInfo(msg:String):void
		{
			_info.htmlText=msg;
		}
		
		
		private function drawVisibleArea():void
		{
			var w:Number=_camera.width*_biliX;
			var h:Number=_camera.height*_biliY;
			_visibleArea.graphics.lineStyle(2,0xfff000);
			_visibleArea.graphics.lineTo(0,0);
			_visibleArea.graphics.lineTo(w,0);
			_visibleArea.graphics.lineTo(w,h);
			_visibleArea.graphics.lineTo(0,h);
			_visibleArea.graphics.lineTo(0,0);
			_visibleArea.graphics.endFill();
		}
		
		/**
		 * 镜头移动事件 
		 */
		protected function onCameraMoving(e:CameraEvent):void
		{
			_visibleArea.x=e.camera.x*_biliX;
			_visibleArea.y=e.camera.y*_biliY;
		}
		
		/**
		 * 镜头大小改变，远近
		 */
		protected function onCameraResize(e:CameraEvent):void
		{
			drawVisibleArea();
		}
		
		//********************8n private********************
		private var _mapData:MapData;
		private var _camera:Camera;
		private var _bitmap:Bitmap;
		private var _biliX:Number;
		private var _biliY:Number;
		/** 可视区域 */
		private var _visibleArea:Sprite;
		private var _info:TextField;//文本提示
		
	}
}